package beanstao.util.id;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import beanstao.util.Sber;
import beanstao.util.Strings;

/**
 * 描述多个Id在一起的包装组合，如:机构的业务就可能表达为机构ID字符串加上业务ID字符串<br>
 * 实现了Id接口的Id类型的集合
 * 
 * @author beanstao@google.com 
 */
public class MultiId implements List<Id>
{
	private List<Id> _List = null;
	private char _First;
	public static final String Separator = "^";

	/**
	 * 快速创建本类型
	 * 
	 * @param ids
	 * @return
	 */
	public static MultiId ME(Id... ids)
	{
		return new MultiId(ids);
	}

	/**
	 * 从一个持久化的本类型字符串解析得到一个本类型
	 * 
	 * @param ids
	 * @return
	 */
	public static MultiId parse(String ids)
	{
		MultiId mid = new MultiId();
		String[] idarray = Strings.splitIgnoreBlank(ids, Separator);
		for (String id : idarray)
			mid.add(DId.ME(id));
		return mid;
	}

	public MultiId()
	{
		_List = new ArrayList<Id>();
	}

	public MultiId(Id... ids)
	{
		_First = ids[0].toString().toLowerCase().charAt(0);
		_List = new ArrayList<Id>();
		this.addAll(ids);
	}

	public char getFirst()
	{
		return _First;
	}

	// ===================

	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + _First;
		result = prime * result + ((_List == null) ? 0 : _List.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (this.getClass() != obj.getClass())
			return false;
		MultiId other = (MultiId) obj;
		if (_List == null)
		{
			if (other._List != null)
				return false;
		}
		else
		{
			if (_List.size() != other.size())
				return false;
			for (int i = 0; i < _List.size(); i++)
				if (!_List.get(i).equals(other.get(i)))
					return false;
		}
		return true;
	}

	/**
	 * 本类型持久化时的表达方式:将所有ID以插入的顺序,用MultiId.Separator进行分隔并相加。<br>
	 * 可以用本类型的parse静态方法反向解析。
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		Sber sber = Sber.ME();
		for (int i = 0; i < _List.size(); i++)
		{
			if (_List.get(i) == null)
				continue;
			sber.append(_List.get(i).toString()).append(Separator);
		}
		return sber.trimEnd().toString();
	}

	// == List实现 =====================

	@Override
	public boolean add(Id e)
	{
		if (_List.size() == 0)
			_First = e.toString().toLowerCase().charAt(0);
		return _List.add(e);
	}

	@Override
	public void add(int index, Id element)
	{
		_List.add(index, element);
	}

	public void addAll(Id[] ids)
	{
		for (Id id : ids)
			this.add(id);
	}

	@Override
	public boolean addAll(Collection<? extends Id> c)
	{
		return _List.addAll(c);
	}

	@Override
	public boolean addAll(int index, Collection<? extends Id> c)
	{
		return _List.addAll(index, c);
	}

	@Override
	public void clear()
	{
		_List.clear();
	}

	@Override
	public boolean contains(Object o)
	{
		return _List.contains(o);
	}

	@Override
	public boolean containsAll(Collection<?> c)
	{
		return _List.containsAll(c);
	}

	@Override
	public Id get(int index)
	{
		return _List.get(index);
	}

	@Override
	public int indexOf(Object o)
	{
		return _List.indexOf(o);
	}

	@Override
	public boolean isEmpty()
	{
		return _List.isEmpty();
	}

	@Override
	public Iterator<Id> iterator()
	{
		return _List.iterator();
	}

	@Override
	public int lastIndexOf(Object o)
	{
		return _List.lastIndexOf(o);
	}

	@Override
	public ListIterator<Id> listIterator()
	{
		return _List.listIterator();
	}

	@Override
	public ListIterator<Id> listIterator(int index)
	{
		return _List.listIterator(index);
	}

	@Override
	public boolean remove(Object o)
	{
		return _List.remove(o);
	}

	@Override
	public Id remove(int index)
	{
		return _List.remove(index);
	}

	@Override
	public boolean removeAll(Collection<?> c)
	{
		return _List.removeAll(c);
	}

	@Override
	public boolean retainAll(Collection<?> c)
	{
		return _List.retainAll(c);
	}

	@Override
	public Id set(int index, Id element)
	{
		return _List.set(index, element);
	}

	@Override
	public int size()
	{
		return _List.size();
	}

	@Override
	public List<Id> subList(int fromIndex, int toIndex)
	{
		return _List.subList(fromIndex, toIndex);
	}

	@Override
	public Object[] toArray()
	{
		return _List.toArray();
	}

	@Override
	public <T> T[] toArray(T[] a)
	{
		return _List.toArray(a);
	}

}
